<?php

/* 2016-01-12 11:49:52 */

class model extends ModelAbstract

{

	public function internet()
	{
		isset($_POST['model']) or die('非法操作！');
		$dir = SYSDIR.LIB.'PHPExcel/';
		$vdb = SYSDIR.VDB.'data/internet.xls';
		require_once $dir.'PHPExcel.php';
		require_once $dir.'PHPExcel/IOFactory.php';
		$inputFileType = 'Excel5';
		$objReader = PHPExcel_IOFactory::createReader($inputFileType);//Excel文件格式不相同报错
		$objPHPExcel = $objReader->load($vdb);
		$sheet = $objPHPExcel->getSheet(0); // 读取第一個工作表
		$highestRow = $sheet->getHighestRow(); // 取得总行数
		$highestColumm = $sheet->getHighestColumn(); // 取得总列数
		$item = $_POST['item'];
		if("ajaxLoad" == $item){
			//$customPropertyList = $objPHPExcel->getProperties()->getCustomProperties();
			//echo $sheet->getCell('A1')->getValue();
			$data = array();
			$column = array("A","B","C","D","E","F","G","H","I","J","K","L");
			$key = 0;
			for($i=2;$i<=$highestRow;$i++){
				foreach($column as $k=>$v){
					$keyValue = $sheet->getCell($v.$i)->getValue();
					$data[$key][$k] = (empty($keyValue)? "":$keyValue);
				}
				$data[$key]["dataRow"] = $i;//xls 实际行
				$key++;
			}
			echo json_encode($data);
		}elseif("setInternet" == $item){
			//print_r($_POST);return;
			$internet = $_POST["internet"];
			$curRow = $highestRow+1;
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$curRow, $internet["site"])
										  ->setCellValue('B'.$curRow, $internet["url"])
										  ->setCellValue('C'.$curRow, $internet["islogin"])
										  ->setCellValue('D'.$curRow, $internet["username"])
										  ->setCellValue('E'.$curRow, $internet["passw"])
										  ->setCellValue('F'.$curRow, $internet["email"])
										  ->setCellValue('G'.$curRow, $internet["cellphone"])
										  ->setCellValue('H'.$curRow, $internet["remark"])
										  ->setCellValue('I'.$curRow, date("Y-m-d H:i:s"))
										  ->setCellValue('J'.$curRow, getIp())
										  ->setCellValue('K'.$curRow,nl2br($internet["explan"]));
			$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,$inputFileType);
			$objWriter->save($vdb);
			echo true;
		}elseif("toHrefCount" == $item){//网站点击量写入
			$row = $_POST["row"];
			$hrefCount = $sheet->getCell("L".$row)->getValue();
			$hrefCount = (empty($hrefCount)? 1 : ($hrefCount+1));
			$objPHPExcel->getActiveSheet()->setCellValue('L'.$row, $hrefCount);
			$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,$inputFileType);
			$objWriter->save($vdb);
			echo $hrefCount;
		}
	}
	public function virtualdb()
	{
		isset($_POST['item']) or die('非法操作！');
		$item = $_POST['item'];
		if($item == 'saveFile'){
			$retVal = '0';
			$json = $_POST;unset($json['model']);unset($json['uid']);unset($item);
			$type = $_POST['ftype'];$fname = $_POST['fname'];
			$fdesc = $_POST['fdesc'];$fstruct = $_POST['fstruct'];
			$fstr_desc = $_POST['fstr_desc'];
			$file = empty($type)? $fname : $fname.'.'.$type;
			$fsearch = VDB.'model/'.$type.'.'.$type;
			$csv = array($file,$fstruct,$fstr_desc);
			$csv = array_merge($csv,array(date('Y-m-d H:i:s'),getIp()));
			$json = array_merge($json,array('date'=>date('Y-m-d H:i:s'),'ip'=>getIp()));
			$csv = implode(",",$csv);
			if(is_file($fsearch) && copy($fsearch,AUTO.$file)){
				$retVal = '文件“'.$file.'”创建成功！';
			}else{
				crtfile(AUTO.$file);
				$retVal = '文件“'.$file.'”生成成功！';
			}
			if($retVal != '0'){
				$this->console("\n".$csv,'N','','virtualdb/model/log.csv');
				unset($json['item']);
				$this->console("\n".serialize($json),'N','','virtualdb/model/design/'.$fname.'.jh','w');
				$syslog = 'ip为 '.getIp().' 的用户新增《'.$file.'》虚拟数据仓文件';
				$this->console($syslog,'','system.log');
			}
			echo $retVal;
		}elseif($item == 'loadFileList'){
			$filelist = $this->getDir(AUTO);
			if(count($filelist) == 0){echo count($filelist);die;}
			$uls = '<ul>';
			foreach($filelist['data'] as $v){
				$uls .= '<li>
					<a href="javascript:void(0);" class="files" _dir="'.$filelist['dir'].$v.'">'.$v.'</a>
						<span><a href="javascript:void(0);" class="delFiles" _dir="'.$filelist['dir'].$v.'">移除</a></span>
				</li>';
			}
			$uls .= '</ul>';
			echo $uls;
		}elseif($item == 'fnameCheck'){
			$fname = AUTO.$_POST['fname'];
			if(is_file($fname)){ echo '1';die;}
			echo $fname;
		}elseif($item == 'delFile'){
			$dir = $_POST['dir'];
			if(empty($dir)) die;
			$finfo = $this->fileInfo($dir);
			unlink(SYSDIR.VDB.'model/design/'.$finfo['perffix'].'.jh');
			if(unlink($dir)){//删除文件
				$syslog = 'ip为 '.getIp().' 的用户删除目录为《'.$dir.'》虚拟数据仓';
				$this->console($syslog,'','system.log');
				echo '1';die;
			}
			echo '0';
		}elseif($item == 'loadModule'){
			$fname = $_POST['fname'];
			//$fdir = $_POST['fdir'];
			$finfo = $this->fileInfo($fname);
			$dsnInfo = SYSDIR.VDB.'model/design/'.$finfo['perffix'].'.jh';
			$contents = SYSDIR.VDB.'model/database/'.$fname;
			$ctStr = '';
			if(is_file($contents)){$ctStr = file_get_contents($contents);$ctStr .= "/n新增加的数据如下:/n";}
			$data = unserialize(trim(file_get_contents($dsnInfo)));//字符串为空时无法序列化
			$act = array('csv'=>'table','text'=>'html','json'=>'tree','xml'=>'text');//定制编辑与预览方式,所有的都包含文本框预览方式
			$fstruct = $data['fstruct'];
			$actXhtml = '';
			if(isset($act[$fstruct])){
				$actXhtml = '<p>预览方式选择:  <a href="javascript:void(0);">text</a> '.($act[$fstruct] == 'text'? '':'<a href="javascript:void(0);">'.$act[$fstruct].'</a>').'</p>';
			}
			if(is_file($dsnInfo)){
				
				$xhtml = '
				<div class="virdb_design"><a href="javascript:void(0);"><h4>文件设置信息</h4></a>
					<p>
						<br>文件名称: '.$fname.'
						<br>文件描述:'.$data['fdesc'].'
						<br>文件结构:'.$fstruct.'，存储格式“<i>'.$data['fstr_desc'].'</i>”
						<br>创建日期:'.$data['date'].'
						<br>来源IP:'.$data['ip'].'
						<p>数据获取于'.date('Y-m-d H:i:s').'</p>
					</p>
				</div>
				<p><a href="javascript:void(0);"><h4>图层扩展</h4></a></p>
				'.$actXhtml .
				(isset($act[$fstruct])? '
				<div id="virdb_extend"></div>
				<div class="writeCode"><textarea class="writCipt"></textarea><br><a href="javascript:void(0);" class="writeSave">保存</a></div>
				<div class="printCode"><textarea class="pritCipt" readonly="readonly">'.$ctStr.'</textarea></div>
				': '');
			}else{
				$xhtml = '<p><i>文件读取失败？<i></p>';
			}
			echo $xhtml;
		}elseif($item == 'writeSave'){
			$fname = $_POST['fname'];
			$str = $_POST['text'];
			$this->console($str,'N','','virtualdb/model/database/'.$fname);
			$contents = SYSDIR.VDB.'model/database/'.$fname;
			$ctStr = '';
			if(is_file($contents)){$ctStr = trim(file_get_contents($contents));}
			echo $ctStr;
		}
	}
}

?>

		